let cnn = require('./dbManager');
let sqlstring = require('sqlstring');
let uuid = require('uuid/v4');

let mm = {
    addContent: function (info, cb) {
        info.contentid = uuid();
        let sf = "insert into t_content set ?"
        let sql = sqlstring.format(sf, info);
        console.log(sql);
        cnn.query(sql, function (err, results) {
            if (err) {
                cb(400);
                console.error(err);
                return;
            }
            cb(200)
            console.log(JSON.stringify(results));
        });
    },
    removeContent: function (info, cb) {
        let sf = "delete from t_content where ??=?"
        let sql = sqlstring.format(sf, info);
        console.log(sql);
        cnn.query(sql, function (err, results) {
            if (err) {
                cb(400);
                console.error(err);
                return;
            }
            console.log(JSON.stringify(results));
        });
    },
    updateContent: function (info, cb) {
        let prarams = [info, "contentid", info['contentid']];
        let sf = "update t_content set ? where ??=?"
        let sql = sqlstring.format(sf, prarams);
        console.log(sql);
        cnn.query(sql, function (err, results) {
            if (err) {
                cb(400);
                console.error(err);
                return;
            }
            cb(200)
            console.log(JSON.stringify(results));
        });
    },
    getContentList: function (info, cb) {
        let pageSize = info['pageSize'];
        if(pageSize == undefined) pageSize = 20;
        let sql = `select * from t_content limit ${info.page},${pageSize}`;
        cnn.query(sql, function (err, results) {
            if (err) {
                cb(400);
                console.error(err);
                return;
            }
            cb(200)
            console.log(JSON.stringify(results));
        });
    },
};

// mm.addContent(
//     {
//         "url": "http://www.douyin.com/001",
//         "product": "张三",
//         "tags": "张三-张-三",
//         "idea_num": 5,
//         "relate_num": 2,
//         "goodsense_num": 5,
//         "source": 1,
//         "uid": "123"
//     }
// )

// mm.updateContent(
//     {
//         "url": "http://www.douyin.com/001",
//         "product": "张三999999",
//         "tags": "张三-张-三",
//         "idea_num": 5,
//         "relate_num": 2,
//         "goodsense_num": 5,
//         "source": 1,
//         "uid": "123",
//         contentid: "fc3623ab-679b-4bce-be41-20c56f524bfe"
//     }
// )

// mm.removeContent(["contentid","5ca92a89-af43-4aa5-a546-3fa58d50e207"])